package com.bwie.controller;
import cn.hutool.core.util.RandomUtil;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.service.impl.UserServiceImpl;
import com.bwie.utils.KaptchaUtils;
import com.bwie.utils.R;
import com.bwie.vo.CodeVo;
import com.bwie.vo.DateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* (t_user)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/user/index")
@Api(tags = "用户列表")
public class UserController {
/**
* 服务对象
*/
    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation("获取图片验证码")
    @GetMapping("/getPictureCode")
    public void getPictureCode(){
        String s = RandomUtil.randomNumbers(4);
        KaptchaUtils.generateCaptcha(response,s);
        redisTemplate.opsForValue().set("图片验证码"+s,s,1, TimeUnit.MINUTES);
        System.out.println("图片验证码是:"+s);
    }

    @ApiOperation("发送手机号验证码")
    @PostMapping("/sendPhoneCode")
    public R sendPhoneCode(@RequestBody CodeVo codeVo){
        return userService.sendPhoneCode(codeVo);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public R login(@RequestBody CodeVo codeVo){
        return userService.login(codeVo);
    }

    @ApiOperation("获取今日入住客人数")
    @PostMapping("/selectAllPeoPle")
    public R selectAllPeople(){
        ArrayList<String> x1 = new ArrayList<>();
        ArrayList<Integer> y1 = new ArrayList<>();

        List<DateVo> dateVoList = userService.selectAllOrder();

        HashMap<String, Object> map = new HashMap<>();

        for (DateVo dateVo : dateVoList) {
            x1.add(dateVo.getXData());
            y1.add(dateVo.getYData());
        }
        map.put("xDate",x1);
        map.put("yDate",y1);
        return R.OK(map);
    }

    @ApiOperation("获取一周内入住和退订人数")
    @PostMapping("selectWeekPeople")
    public R selectWeekPeople(){
        ArrayList<String> x = userService.selectWeekPeople();
        ArrayList<Integer> y1 = userService.selectWeekPeopleY1();
        ArrayList<Integer> y2 = userService.selectWeekPeopleY2();
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y1",y1);
        map.put("y2",y2);
        return R.OK(map);
    }
}
